#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "common.h"

#define MAX_TOKENS 128

int table_equals(char * * lhs, char * * rhs) 
{
  while (*lhs != 0 && *rhs != 0)  {
    if (strcmp(*lhs, *rhs) != 0)
      return 0;
    lhs++;
    rhs++;
  }
  return *lhs == *rhs;
}

void test_split() 
{
  char * * tokens = malloc(MAX_TOKENS * sizeof(char));  
  char * input0 = "";
  char * result0 [] = {NULL};
  char * input1 = "ala ma kota";
  char * result1 [] = {"ala", "ma", "kota", NULL};
  char * input2 = "ala ma kota|a kot ma ale";
  char * result2 [] = {"ala ma kota", "a kot ma ale", NULL};
  
  split(input0, " ", tokens);
  assert(table_equals(tokens, result0));

  split(input1, " ", tokens);
  assert(table_equals(tokens, result1));

  split(input2, "|", tokens);
  assert(table_equals(tokens, result2));


  free(tokens);
}

void assert_copy_token(char * string, char * separators, char * expected_token)
{
  char * token;
  token  = copy_token(string, separators);
  assert(strcmp(token, expected_token) == 0);
  free(token);
}

void test_copy_token() 
{
  assert(strcmp(copy_token("asdas das das das", " "), "asdas") == 0);
  assert(strcmp(copy_token("asdas das|das das", "|"), "asdas das") == 0);
  assert(strcmp(copy_token("waww", " "), "waww") == 0);
  assert(strcmp(copy_token("", " "), "") == 0);
}

int main() 
{
    test_copy_token();
    test_split();
    printf("OK\n");
    return 0;
}
